package com.axend.aerosense.common.ui;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.axend.aerosense.common.R;
import com.axend.aerosense.common.adapter.CommonUserListAdapter;
import com.axend.aerosense.common.bean.HomeReportBean;
import com.axend.aerosense.common.databinding.CommonUserSelectListBinding;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class CustomUserSelectPopupWindow extends BasePopupWindow<CommonUserSelectListBinding> {

    /** 最大的弹窗高度（dp） */
    private static final int MAX_POPUP_WINDOW_HEIGHT_DP = 436;

    private Builder builder;
    private CommonUserListAdapter listAdapter;

    public CustomUserSelectPopupWindow(Builder builder) {
        super(builder.activity, builder.layoutRes);
        this.builder = builder;

        //此处距离屏幕两边各15dp
        int windowWidth = getDisplayMetrics().widthPixels;
        mInstance.setWidth(builder.width < 0 ? builder.width : windowWidth - SizeUtils.dp2px(30));
        mInstance.setHeight(builder.height);
        mInstance.setAnimationStyle(R.style.common_anim_home_list_popup_window);
        mInstance.setOutsideTouchable(true);
        mInstance.setFocusable(true);
        setShowBackgroundMask(true);
        setParentView(builder.parentView);
    }

    @Override
    protected void initView() {
        listAdapter = new CommonUserListAdapter();
        //item点击事件
        listAdapter.setOnItemClickListener((adapter, view, position) -> {
            listAdapter.setSelectItem(position);
            callbackSelected(listAdapter.getItem(position));
            dismiss();
        });
        binding.commonRvUserList.setAdapter(listAdapter);
        binding.commonRvUserList.setLayoutManager(new LinearLayoutManager(getActivity()));
        //关闭按钮
        binding.commonIvClose.setOnClickListener(v -> dismiss());
    }

    private void callbackSelected(HomeReportBean reportBean){
        if (null == builder.selectedListener){
            return;
        }

        if (null == reportBean) {
            return;
        }

        builder.selectedListener.onSelected(reportBean);
    }

    /** 更新列表数据 */
    public void setListData(List<HomeReportBean> reportList){
        if (ObjectUtils.isEmpty(reportList)) {
            return;
        }

        listAdapter.setList(reportList);
        //更新数据后，回调之前选中的item
        int selectItem = listAdapter.getSelectItem();
        callbackSelected(listAdapter.getItem(selectItem));
    }


    /**
     * 显示之前需要动态计算高度，列表中最多显示5个item，不足5个按自适应处理
     * 如果弹窗的最大高度比屏幕还大，那么弹窗的高度需要计算（屏幕高度 - view距离屏幕边缘的y坐标值 - 导航栏高度（50dp））
     * @param anchor
     */
    @Override
    public void showAsDropDown(View anchor) {
        int[] location = new int[2];
        anchor.getLocationOnScreen(location);

        //根据数据item个数来判断
        if (listAdapter.getItemCount() >= 5) {
            int screenHeightPixels = getDisplayMetrics().heightPixels;
            if (screenHeightPixels - location[1] > SizeUtils.dp2px(MAX_POPUP_WINDOW_HEIGHT_DP)) {
                mInstance.setHeight(SizeUtils.dp2px(MAX_POPUP_WINDOW_HEIGHT_DP));
            } else {
                //如果屏幕比最大的弹窗高度小，则动态计算
                mInstance.setHeight(SizeUtils.dp2px(MAX_POPUP_WINDOW_HEIGHT_DP - 50) - location[1]);
            }
        } else {
            mInstance.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        super.showAsDropDown(anchor, 0, -anchor.getHeight());
    }

    private DisplayMetrics getDisplayMetrics(){
        return getActivity().getResources().getDisplayMetrics();
    }

    public static Builder builder(Activity activity) {
        return new Builder(activity);
    }

    public static class Builder{

        private Activity activity;
        //布局
        private int layoutRes;
        //宽度
        private int width;
        //高度
        private int height;
        //父布局
        private View parentView;
        //选中回调
        private OnSelectedListener selectedListener;

        public Builder(Activity activity) {
            this.activity = activity;
        }

        public Builder setLayoutRes(int layoutRes) {
            this.layoutRes = layoutRes;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setParentView(View parentView) {
            this.parentView = parentView;
            return this;
        }

        public Builder setSelectedListener(OnSelectedListener listener){
            this.selectedListener = listener;
            return this;
        }

        public CustomUserSelectPopupWindow build(){
            return new CustomUserSelectPopupWindow(this);
        }
    }

    /** 选中项回调 */
    public interface OnSelectedListener{
        void onSelected(HomeReportBean reportBean);
    }
}
